package com.ocean.security.handler;

import com.ocean.core.util.constants.ConstantsAttribute;
import com.ocean.core.util.response.ResultSet;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 无权限访问
 *
 * @author zhang
 * @since 2021/9/10
 */
public class RestAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                       AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setHeader("Content-Type", "application/json;charset=utf-8");
        httpServletResponse.setStatus(200);
        httpServletResponse.getWriter().print(ResultSet.error(ConstantsAttribute.PERMISSION_DENIED));
        httpServletResponse.getWriter().flush();
        httpServletResponse.getWriter().close();
    }
}
